const Options = require('./Options')
const Base = require('./Base')

class Cription extends Base {
  constructor() {
    super()
  }
  createData(dataStr, mac = '') {
    const macArr = mac.match(/[0-9a-f]{2}/ig) || []
    let offset = macArr[macArr.length - 2] ^ this.getDataId()
    this.offset = offset
    const mapsCount = Options.length
    const dataArr = this.stringToByte(dataStr)
    let i = 0
    let data = []
    while (data.length < dataArr.length) {
      if (offset > mapsCount ) {
        offset = 0
      }
      const bit = dataArr[i] ^ Options[offset]
      data.push(bit)
      offset++
      i++
    }
    this.offsetHex = (offset).toString(16).padStart(2, 0)
    const hexData = data.map(x => x.toString(16).padStart(2, 0)).join('')
    return hexData
  }
  formatWriteValue(dataStr, mac = '') {
    const writeContent = this.createData(dataStr, mac)
    const _dataId = this.getDataId()
    let writeContentArr = []
    let start = 0
    let step = 0
    while (start <= writeContent.length) {
      if (start === 0) {
        writeContentArr.push(_dataId + (step).toString(16).padStart(2, 0) + this.offsetHex + writeContent.substring(0, 34))
        start += 34
      } else {
        writeContentArr.push(_dataId + (step).toString(16).padStart(2, 0) + writeContent.substring(start, start + 36))
        start += 36
      }
      step ++
    }
    return writeContentArr
  }
  decodeData(hex) {
    console.log(hex)
  }
}
const advertisData = '01f002fc1a000100030000000758cf791ee6680020'
const dataStr = JSON.stringify({
  CID: 30005,
  URL: 'https://oghafnxkic.execute-api.us-west-2.amazonaws.com/Prod',
  PL: {
    SSID: 'granwin',
    Password: '12345678',
  }
})
module.exports = Cription